//
//  UpTableViewCell.m
//  UI_19_网络解析_汽车之家
//
//  Created by li cheng on 16/8/10.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "UpTableViewCell.h"
#import "CollectionViewCell.h"
#import "HotCarList.h"
#import "UIImageView+DownloadImage.h"
@interface UpTableViewCell()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *hotCatArray;
@end
@implementation UpTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
   
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        self.collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
        self.collectionView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:_collectionView];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        [self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"CollectionViewCell"];
        [self hotCarHandleDate];
    }
    return self;
}
- (void)hotCarHandleDate{
//    _hotCatArray = [NSMutableArray array];
//    NSString *hotCarPath = [[NSBundle mainBundle] pathForResource:@"HotCarJSON" ofType:@"json"];
    NSURLSession *URLSession = [NSURLSession sharedSession];
    NSURL *url = [NSURL URLWithString:@"http://cars.app.autohome.com.cn/dealer_v6.0.0/dealer/hotbrands-pm1.json"];
    NSMutableURLRequest *URLRequest = [NSMutableURLRequest requestWithURL:url];
    URLRequest.HTTPMethod = @"GET";
    NSURLSessionDataTask *dataTask = [URLSession dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        dispatch_sync(dispatch_get_main_queue(), ^{
            NSError *error = nil;
            _hotCatArray = [NSMutableArray array];
            NSDictionary *hotCarDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
            NSArray *listArray = hotCarDictionary[@"result"][@"list"];
            for (NSDictionary *inListArray in listArray) {
                HotCarList *hotCarList = [[HotCarList alloc] init];
                [hotCarList setValuesForKeysWithDictionary:inListArray];
                [_hotCatArray addObject:hotCarList];
            }
            [_collectionView reloadData];
        });
        [_collectionView reloadData];

    }];
    [_collectionView reloadData];

    [dataTask resume];
}
- (void)layoutSubviews{
    [super layoutSubviews];
    _collectionView.frame = self.contentView.bounds;
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _hotCatArray.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];
    [cell.myImageView asyncDownloadImageWithURL:[_hotCatArray[indexPath.row] img] completionHandler:^(NSData *data, UIImage *image) {
        
    }];
    return cell;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
